package com.wx.controller.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wx.common.*;
import com.wx.constants.Constants;
import com.wx.domain.vo.EveryDayFollowNumber;
import com.wx.domain.vo.PatientFollowVo;
import com.wx.entity.SysPatient;
import com.wx.entity.SysPatientFollow;
import com.wx.exception.BusinessException;
import com.wx.holder.WxUserHolder;
import com.wx.service.SysPatientFollowService;
import com.wx.service.SysPatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * <p>Title:      ApiPatientFollowController. </p>
 * <p>Description  随访管理</p>
 * <p>Copyright: Copyright (c) 2016</p>
 * <p>Company:    医百科技有限公司 </p>
 *
 * @author         yangwensheng@100doc.com.cn
 * @since      2021-03-03 11:36:07
 */
@RestController
@RequestMapping(value = "/api/sysPatient/follow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@Api(tags = "随访管理(API)", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ApiPatientFollowController {

    @Resource
    private SysPatientFollowService sysPatientFollowService;

    @Resource
    private SysPatientService sysPatientService;

    @Resource
    private WxUserHolder wxUserHolder;


    @PostMapping("/list/page")
    @ApiOperation(httpMethod = "POST", value = "根据日期查询随访列表")
    public Wrapper<PageInfo<PatientFollowVo>> listPage(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody PageParam<SysPatientFollow> pageParam) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        PageInfo<PatientFollowVo> list = sysPatientFollowService.listPage(pageParam,userInfo.getOpenid());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @PostMapping("/list/page/in/group")
    @ApiOperation(httpMethod = "POST", value = "分页查询在组患者列表")
    public Wrapper<PageInfo<SysPatient>> listPageInGroupByOpenId(@ApiParam(name = "SysPatient", value = "SysPatient") @RequestBody PageParam<SysPatient> pageParam) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysPatient param = pageParam.getParam();
        param.setAuditStatus(Constants.YES);
        PageInfo<SysPatient> list = sysPatientService.listPageByOpenId(pageParam,userInfo.getOpenid(),Constants.YES);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @PostMapping("/list/page/out/group")
    @ApiOperation(httpMethod = "POST", value = "分页查询脱组患者列表")
    public Wrapper<PageInfo<SysPatient>> listPageOutGroupByOpenId(@ApiParam(name = "SysPatient", value = "SysPatient") @RequestBody PageParam<SysPatient> pageParam) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        SysPatient param = pageParam.getParam();
        param.setAuditStatus(Constants.TWO);
        PageInfo<SysPatient> list = sysPatientService.listPageByOpenId(pageParam,userInfo.getOpenid(), Constants.OUT_GROUP_STATUS);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
    }


    @PostMapping("/list/page/patient")
    @ApiOperation(httpMethod = "POST", value = "根据患者id查询随访记录")
    public Wrapper<PageInfo<PatientFollowVo>> listPageByPatientId(@RequestBody PageParam<SysPatientFollow> pageParam) {
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<PatientFollowVo> list = sysPatientFollowService.listPageByPatientId(pageParam.getParam());
        PageInfo<PatientFollowVo> pageInfo = new PageInfo<>(list);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, pageInfo);
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "删除随访记录")
    public Wrapper<Integer> deletePatientFollow(@PathVariable("id") String id) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        int result = sysPatientFollowService.deletePatientFollow(id, userInfo.getOpenid(), userInfo.getNickname());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, result);
    }

    @PostMapping("/add/log")
    @ApiOperation(httpMethod = "POST", value = "新增随访日程")
    public Wrapper<?> addPatientFollowLog(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody @Validated(CreateValidate.class) SysPatientFollow sysPatientFollow) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatientFollow.setCreator(userInfo.getNickname());
        sysPatientFollow.setCreateBy(userInfo.getOpenid());
        sysPatientFollow.setUpdater(userInfo.getNickname());
        sysPatientFollow.setUpdateBy(userInfo.getOpenid());
        sysPatientFollowService.addPatientFollowLog(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE);
    }

    @PostMapping("/update")
    @ApiOperation(httpMethod = "POST", value = "更新随访记录")
    public Wrapper<?> update(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody @Validated(UpdateValidate.class) SysPatientFollow sysPatientFollow) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatientFollow.setUpdater(userInfo.getNickname());
        sysPatientFollow.setUpdateBy(userInfo.getOpenid());
        sysPatientFollow.setUpdateDate(new Date());
        int result = sysPatientFollowService.updateFollow(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }

    @PostMapping("/every/day/user/number")
    @ApiOperation(httpMethod = "POST", value = "每日随访用户数量(月随访日历)")
    public Wrapper<List<EveryDayFollowNumber>> queryEveryDayFollowNumber(@RequestBody SysPatientFollow sysPatientFollow) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        List<EveryDayFollowNumber> result = sysPatientFollowService.queryEveryDayFollowNumber(userInfo.getOpenid(),sysPatientFollow.getFollowTime());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @GetMapping("/next/follow/time/{patientId}")
    @ApiOperation(httpMethod = "GET", value = "查询患者的下次随访日期")
    public Wrapper<String> queryNextFollowTimeByPatientId(@PathVariable("patientId") String patientId) {
        String result = sysPatientFollowService.queryNextFollowTimeByPatientId(patientId);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据id查询随访记录")
    public Wrapper<SysPatientFollow> get(@PathVariable("id") String id) {
        SysPatientFollow sysPatientFollow = sysPatientFollowService.selectByKey(id);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,sysPatientFollow);
    }


    @PostMapping("/add/inquiry")
    @ApiOperation(httpMethod = "POST", value = "新增问询")
    public Wrapper<?> addPatientInquiry(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody @Validated(CreateValidate.class) SysPatientFollow sysPatientFollow) {
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatientFollow.setCreator(userInfo.getNickname());
        sysPatientFollow.setCreateBy(userInfo.getOpenid());
        sysPatientFollow.setUpdater(userInfo.getNickname());
        sysPatientFollow.setUpdateBy(userInfo.getOpenid());
        int result = sysPatientFollowService.addPatientInquiry(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


    @PostMapping("/update/inquiry")
    @ApiOperation(httpMethod = "POST", value = "更新问询")
    public Wrapper<?> updatePatientInquiry(@ApiParam(name = "sysPatientFollow", value = "sysPatientFollow") @RequestBody @Validated(UpdateValidate.class) SysPatientFollow sysPatientFollow) {
        if(StringUtils.isEmpty(sysPatientFollow.getId())){
            throw new BusinessException("id不能为空");
        }
        WxOAuth2UserInfo userInfo = wxUserHolder.getUserInfo();
        sysPatientFollow.setUpdater(userInfo.getNickname());
        sysPatientFollow.setUpdateBy(userInfo.getOpenid());
        int result = sysPatientFollowService.update(sysPatientFollow);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE,result);
    }


}